package com.ncs.util;

import com.ncs.pojo.Product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author yinyin
 * @create 2021/07/15 下午 7:21
 */
public class MybatisUtil {
    private MybatisUtil(){
    };
    private static SqlSessionFactory factory;

    static{
        InputStream inputStream = null;
        //读取配置文件
        try {
            inputStream = Resources.getResourceAsStream("source/mybatis-config.xml");
            //创建会话工厂
            factory = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (IOException e) {
            new RuntimeException("找不到mybatis-config.xml配置文件");
        }
    }
    //获取sqlSession
    public static   SqlSession getSession(){
          return factory.openSession();
    }
    //关闭资源
    public static void closeSession(SqlSession session){
            session.close();

    }
}
